<?php //TEST THIS...i think we done!
class Account_Aggregate extends Generic_Record {
    public static function initialize($db) {
        parent::initialize($db,'account_aggregate');
    }
    
    protected static $resolutions = array('total','year','month','day');
    protected static $defaults = array('cents'=>0);

    public static function modify ($account_id, $date, $value) {
        if ($account_id) {
            $rows = static::fetch_for_date($account_id,$date);
            $dates = static::get_dates($date);
            foreach (static::$resolutions as $res) {
                if (!isset($rows[$res])) {
                    $new_info = static::$defaults;
                    $new_info['account_id'] = $account_id;
                    $new_info['resolution'] = $res;
                    $new_info['date'] = $dates[$res];
                    $rows[$res] = $row = static::create($new_info);
                } else $row = $rows[$res];
                $row->set('cents',$row->get('cents')+$value);
                $row->save();
            }
        }
    }
    
    public static function update ($old, $new) {
        if ($old) {
            $date = $old->get('date');
            $cents = $old->get('cents');
            static::modify($old->get('from_account_id'),$date,$cents);
            static::modify($old->get('to_account_id'),$date,-$cents);
        }
        if ($new) {
            $date = $new->get('date');
            $cents = $new->get('cents');
            static::modify($new->get('from_account_id'),$date,-$cents);
            static::modify($new->get('to_account_id'),$date,$cents);
        }
    }
    
    public static function get_dates($date)
        $date = strtotime($date);
        $d = date('d',$date);
        $m = date('m',$date);
        $y = date('Y',$date);
        return array(
            'total' => null;
            'year' => strtotime("$y-01-01"),
            'month' => strtotime("$y-$m-01"),
            'day' => strtotime("$y-$m-$d")
        );
    }
    
    public static function get_balance($account_id,$before_date = null) {
        $query = "SELECT sum(cents) FROM ".self::$table." WHERE account_id = $account_id";
        if (isset($before_date)) {
            $dates = static::get_dates($date);
            $query .= " AND (
                (resolution = 'year' AND date < '{$dates['year']}')
                OR (resolution = 'month' AND date < '{$dates['month']}' AND date >= '{$dates['year']}')
                OR (resolution = 'day' AND date < '{$dates['day']}' AND date >= '{$dates['month']}')
            )";
        } else {
            $query .= " AND resolution = 'total'";
        }
        $table = static::$table;
        $q = new MysqlQuery($table->db_link,$query);
        $balance = $q->get_atom();
        return isset($balance) ? $balance : 0;
    }
    
    public static function fetch_for_date($account_id,$date) {
        $dates = static::get_dates($date);
        $where = "
            account_id = $account_id AND (
                (resolution = 'year' AND date = '{$dates['year']}')
                OR (resolution = 'month' AND date = '{$dates['month']}')
                OR (resolution = 'day' AND date = '{$dates['day']}')
                OR resolution = 'total'
            )";
        $rows = array();
        foreach (static::load_multiple($where) as $row) {
            $rows[$row->get('resolution')] = $row;
        }
        return $rows;
    }
    
}
?>